#!/bin/sh

[ ${SYNOPKG_PKGDEST} ] || SYNOPKG_PKGDEST=`ls -l /var/packages/tvstreamrecord/target | cut -d\> -f2 | cut -d\  -f2`

findpython() {
    TVSTREAMRECORD_PYTHON='python'
    type -P ${TVSTREAMRECORD_PYTHON} &> /dev/null || {
        TVSTREAMRECORD_PYTHON='/volume1/@appstore/python/bin/python'
        type -P ${TVSTREAMRECORD_PYTHON} &> /dev/null || {
            TVSTREAMRECORD_PYTHON='/usr/local/python26/bin/python'
            type -P ${TVSTREAMRECORD_PYTHON} &> /dev/null || {
                exit 1
            }
        }
    }
}



stop() {
	PID=`ps -w | grep 'python [t]vstreamrecord.py' | cut -c 1-5`
    kill ${PID}
	sleep 3
	kill -2 ${PID}	
	sleep 3
	kill -9 ${PID}	
    exit 0
}

case $1 in
	start)
		cd ${SYNOPKG_PKGDEST}
        findpython        
        nohup ${TVSTREAMRECORD_PYTHON} tvstreamrecord.py >/dev/null 2>&1 &
        exit 0
	;;
	stop)	stop
			;;
	status)
		exit 0
	;;
	log)
		echo "tvstreamrecord is installed properly. To access it, click icon under 'admin' menu">/tmp/cc.log
		echo "/tmp/cc.log";
		exit 0
	;;
esac
                                     
